﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using qlbansach_thuctapnhom.DAL;
namespace qlbansach_thuctapnhom.BLL
{
    public class khachhang_bll
    {
        quanlybansachDataContext ql = new quanlybansachDataContext();
        //==============================================
        public bool kiemtrakhachhang(string makh)
        {
            bool ok = ql.khachhangs.Where(k => k.makh == makh).FirstOrDefault() != null ? true : false;
            if (!ok)
                return true;
            else
                return false;
        }
        //==============================================
        public bool themkhachhang(khachhang kh)
        {
            try
            {
                ql.khachhangs.InsertOnSubmit(kh);
                ql.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        //==============================================
        public bool suakhachhang(khachhang kh)
        {
            try
            {
                ql.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        //=====================================================================
        public void xoakhachhang(string makh)
        {
            khachhang kh = ql.khachhangs.Where(a=>a.makh==makh).FirstOrDefault();
            donhang don = ql.donhangs.Where(k => k.makh == kh.makh).FirstOrDefault();//tim don hang co ma khach hang can xoa
            chitiet ct;
            if (don!=null)
            {
                ct = ql.chitiets.Where(c => c.madh == don.madonhang).FirstOrDefault();//tim chitiet don hang co ma khach hang can xoa
                ql.chitiets.DeleteOnSubmit(ct);
                ql.donhangs.DeleteOnSubmit(don);
            }
            ql.khachhangs.DeleteOnSubmit(kh);
            ql.SubmitChanges();
        }
        //=======================================================================
        public bool timkiemma(string makh)
        {
            bool check = ql.khachhangs.Where(khach=>khach.makh==makh).FirstOrDefault()!=null?true:false;
            return check;
        }
        public bool timkiemten(string ten)
        {
            bool check = ql.khachhangs.Where(khach => khach.ten == ten).FirstOrDefault() != null ? true : false;
            return check;
        }
        public bool timkiememail(string email)
        {
            bool check = ql.khachhangs.Where(khach => khach.email == email).FirstOrDefault() != null ? true : false;
            return check;
        }
        public bool timkiemsdt(string sdt)
        {
            bool check = ql.khachhangs.Where(khach => khach.sđt == sdt).FirstOrDefault() != null ? true : false;
            return check;
        }
        public bool timkiemdiachi(string diachi)
        {
            bool check = ql.khachhangs.Where(khach => khach.diachi == diachi).FirstOrDefault() != null ? true : false;
            return check;
        }

    }
}
